package com.zzyl.web.controller.member;

import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.nursing.domain.dto.UserLoginRequestDto;
import com.zzyl.nursing.domain.pojo.FamilyMember;
import com.zzyl.nursing.domain.vo.LoginVo;
import com.zzyl.nursing.service.IFamilyMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 老人家属Controller
 *
 * @author alexis
 * @date 2025-05-26
 */
@Api(tags = "老人家属管理")
@RestController
@RequestMapping("/member/user")
@Slf4j
public class FamilyMemberController extends BaseController {
    @Autowired
    private IFamilyMemberService familyMemberService;

    /**
     * 查询老人家属列表
     */
    @ApiOperation("查询老人家属列表")
    @PreAuthorize("@ss.hasPermi('nursing:familyMember:list')")
    @GetMapping("/list")
    public TableDataInfo<List<FamilyMember>> list(@ApiParam("老人家属查询条件") FamilyMember familyMember) {
        startPage();
        List<FamilyMember> list = familyMemberService.selectFamilyMemberList(familyMember);
        return getDataTable(list);
    }

    /**
     * 导出老人家属列表
     */
    @ApiOperation("导出老人家属列表")
    @PreAuthorize("@ss.hasPermi('nursing:familyMember:export')")
    @Log(title = "老人家属", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@ApiParam(value = "老人家属查询条件") HttpServletResponse response, FamilyMember familyMember) {
        List<FamilyMember> list = familyMemberService.selectFamilyMemberList(familyMember);
        ExcelUtil<FamilyMember> util = new ExcelUtil<FamilyMember>(FamilyMember.class);
        util.exportExcel(response, list, "老人家属数据");
    }

    /**
     * 获取老人家属详细信息
     */
//    @ApiOperation("获取老人家属详细信息")
//    @PreAuthorize("@ss.hasPermi('nursing:familyMember:query')")
//    @GetMapping(value = "/{id}")
//    public R<FamilyMember> getInfo(@ApiParam("老人家属ID") @PathVariable("id") Long id) {
//        return R.ok(familyMemberService.selectFamilyMemberById(id));
//    }

    /**
     * 新增老人家属
     */
    @ApiOperation("新增老人家属")
    @PreAuthorize("@ss.hasPermi('nursing:familyMember:add')")
    @Log(title = "老人家属", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@ApiParam("老人家属信息") @RequestBody FamilyMember familyMember) {
        return toAjax(familyMemberService.insertFamilyMember(familyMember));
    }

    /**
     * 修改老人家属
     */
    @ApiOperation("修改老人家属")
    @PreAuthorize("@ss.hasPermi('nursing:familyMember:edit')")
    @Log(title = "老人家属", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@ApiParam("老人家属信息") @RequestBody FamilyMember familyMember) {
        return toAjax(familyMemberService.updateFamilyMember(familyMember));
    }

    /**
     * 删除老人家属
     */
    @ApiOperation("删除老人家属")
    @PreAuthorize("@ss.hasPermi('nursing:familyMember:remove')")
    @Log(title = "老人家属", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@ApiParam("老人家属ID数组") @PathVariable Long[] ids) {
        return toAjax(familyMemberService.deleteFamilyMemberByIds(ids));
    }

    /**
     * 小程序登录
     */
    @ApiOperation("小程序登录")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody UserLoginRequestDto userLoginRequestDto) {
        LoginVo loginVo = familyMemberService.login(userLoginRequestDto);
        return success(loginVo);
    }

    @GetMapping("/my")
    public AjaxResult getMy() {
        log.info("FamilyMemberController当前线程==>{}", Thread.currentThread()
                .getId());
        familyMemberService.getMy();
        return AjaxResult.success("成功");
    }
}
